<template>
  <div @click="handlerClick" class="yt-open-file use-weixin">
    <slot></slot>
  </div>
</template>
<script>
import { isImg, isVideo } from '@/common/plugin/utils'

const getFileType = (url) => {
  let resault = url.substring(url.lastIndexOf('.') + 1)
  return resault
}

export default {
  name: 'yt-openFile',
  props: {
    /**
     * 文件的路径
     */
    url: String,
    /**
     * 文件的名称
     */
    title: String,
    /**
     * 文件不存在时提示文字
     */
    errorText: {
      type: String,
      default: '暂无附件!'
    }
  },
  methods: {
    handlerClick() {
      if (isImg(this.url) || isVideo(this.url)) {
        this.$router.$push({
          path: '/base/play',
          query: { name: this.title, url: this.url }
        })
      } else if (getFileType(this.url) === 'txt') {
        this.$router.$push({
          path: '/base/previewTxt',
          query: { name: this.title, url: this.url }
        })
      } else {
        window.open(this.url)
      }
    }
  }
}
</script>
